package com.czmh.etc.onlinepubsys.controller;

import cn.hutool.core.bean.BeanUtil;
import com.czmh.etc.common.core.annotation.ApiVersion;
import com.czmh.etc.common.core.utils.JwtUtil;
import com.czmh.etc.common.core.utils.Sequence;
import com.czmh.etc.onlinepubsys.json.JsonResult;
import com.czmh.etc.onlinepubsys.json.mingchuang.JSONResult;
import com.czmh.etc.onlinepubsys.jy.utils.PageUtils;
import com.czmh.etc.onlinepubsys.jy.utils.Query;
import com.czmh.etc.onlinepubsys.model.UserFeedback;
import com.czmh.etc.onlinepubsys.model.UserFeedbackParam;
import com.czmh.etc.onlinepubsys.services.UserFeedbackService;
import com.czmh.etc.onlinepubsys.utils.mingchuang.ReqUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.weixin4j.util.TokenUtil;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * @author Auto
 * @email Auto
 * @date 2020-09-29 13:59:29
 */
@RestController
@RequestMapping("/{version}/api/userfeedback/")
@Api(tags = "用户反馈接口")
public class UserFeedbackController {
    @Autowired
    private UserFeedbackService userFeedbackService;

    /**
     * 列表
     */
    @ApiVersion(1)
    @RequestMapping(value = "/list", method = {RequestMethod.POST})
    @ApiOperation(value = "用户反馈列表", notes = "用户反馈列表")
    public JsonResult list(@RequestBody UserFeedbackParam userFeedbackParam) {
        JsonResult result = new JsonResult();
        //查询列表数据
        if(StringUtils.isEmpty(userFeedbackParam.getUserId())){
            result.setCode(JSONResult.FAIL);
            result.setData("userId不能为空！");
            return result;
        }
        Map<String, Object> params = BeanUtil.beanToMap(userFeedbackParam);
        Query query = new Query(params);
        List<UserFeedback> userFeedbackList = userFeedbackService.queryList(query);
        PageUtils pageUtil = new PageUtils(userFeedbackList);

        result.setCode(JSONResult.SUCCESS);
        result.setData(pageUtil);
        return result;
    }


    /**
     * 信息
     */
    @ApiVersion(1)
    @RequestMapping(value = "/info", method = {RequestMethod.POST})
    @ApiOperation(value = "查询用户反馈信息", notes = "查询用户反馈信息")
    public JsonResult info(@RequestBody UserFeedbackParam userFeedbackParam) {
        UserFeedback userFeedback = userFeedbackService.queryObject(userFeedbackParam.getId());
        JsonResult result = new JsonResult();
        if(userFeedback == null){
            result.setCode(JSONResult.FAIL);
            result.setData("反馈不存在！");
            return result;
        }
        result.setCode(JSONResult.SUCCESS);
        result.setData(userFeedback);
        return result;
    }

    /**
     * 保存
     */
    @ApiVersion(1)
    @PostMapping(value = "save")
    @ApiOperation(value = "提交用户反馈信息", notes = "提交用户反馈信息")
    public JsonResult save(@RequestBody UserFeedbackParam userFeedbackParam) {
        UserFeedback userFeedback = new UserFeedback();
        userFeedback.setName(userFeedbackParam.getName());
        userFeedback.setPhone(userFeedbackParam.getPhone());
        userFeedback.setContent(userFeedbackParam.getContent());
        userFeedback.setImages(userFeedbackParam.getImages());
        userFeedback.setUserId(userFeedbackParam.getUserId());
        userFeedback.setRemark("");
        userFeedback.setId(Sequence.getId());
        userFeedback.setCreateTime(new Date());
        userFeedback.setUpdatedTime(new Date());
        userFeedback.setStatus(0);
        userFeedback.setDeleteFlag(0);
        userFeedback.setCreateUserId(userFeedback.getUserId());
        int result = userFeedbackService.save(userFeedback);
        if (result > 0) {
            JsonResult r = new JsonResult();
            r.setCode(JSONResult.SUCCESS);
            r.setData(userFeedback);
            return r;
        } else {
            JsonResult r = new JsonResult();
            r.setCode(JSONResult.FAIL);
            r.setMessage("问题反馈提交失败");
            return r;
        }
    }

}
